10243. Book 1
Implement a structure Book.
struct Book
{
int
book_id;
char title[50];
char author[50];
char subject[50];
};
Input.
The first line contains the number of records n (n ≤ 100). n records follow, each field is
specified on a separate line. Then follows the number of queries q (q ≤ 100). Each request
contains book_id of the
book.
Output. For each query print the title and the author of the book as shown in
the example. If no such book exists, print “Book not found”.
Sample input |
Sample output |
|
4 234 C_Programming Nuha Ali C Programming Tutorial 12 Telecom Billing Zara Ali Telecom Billing Tutorial 7865 Effective Java Joshua Bloch Java Programming 4321 Design Patterns Elisabeth Freeman Java Programming 3 12 13 4321 |
Telecom Billing by Zara Ali Book not found Design Patterns by Elisabeth Freeman |
structures
Consider several ways to solve this task:
·
using
a static array;
·
using
dynamic memory allocation;
·
using
a linked list;
Algorithm realization
Declare a structure Book.
struct Book
{
int book_id;
char title[50];
char author[50];
char subject[50];
};
Declare an
array of books.
Book book[100];
Read the input
data. Store the information
about books in the array of
structures book.
scanf("%d", &n);
for (i = 0;
i < n; i++)
{
scanf("%d\n", &book[i].book_id);
gets(book[i].title);
gets(book[i].author);
gets(book[i].subject);
}
Read the
number of queries q.
scanf("%d", &q);
for (i = 0;
i < q; i++)
{
Read the book_id of the book, which information should be printed.
scanf("%d", &book_id);
Set flag = 0 if the required book is not found and flag = 1 otherwise.
int flag = 0;
Iterate through the
available books. Find a book with
the identification number book_id.
for (j = 0; j < n; j++)
{
If the required book is found, print its title and
author.
if (book_id == book[j].book_id)
{
printf("%s by %s\n",
book[j].title, book[j].author);
flag = 1;
break;
}
}
If there is no book in the array of structures, print information about
it.
if (flag == 0) printf("Book
not found\n");
}
Algorithm realization – dynamic memory
allocation
#include <stdio.h>
struct Book
{
int book_id;
char title[50];
char author[50];
char subject[50];
};
int q, i,
j, n, book_id;
Book *book;
int main()
{
scanf("%d", &n);
book = new Book[n];
for (i = 0; i < n; i++)
{
scanf("%d\n", &book[i].book_id);
gets(book[i].title);
gets(book[i].author);
gets(book[i].subject);
}
scanf("%d", &q);
for (i = 0; i < q; i++)
{
scanf("%d", &book_id);
int flag = 0;
for (j = 0; j < n; j++)
{
if (book_id == book[j].book_id)
{
printf("%s by %s\n",
book[j].title, book[j].author);
flag = 1;
break;
}
}
if (flag == 0) printf("Book
not found\n");
}
delete[] book;
return 0;
}
Algorithm realization – linked list
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct Book
{
int book_id;
char title[50];
char author[50];
char subject[50];
struct Book *next;
};
struct List
{
public:
Book *head, *tail;
};
int q, i, j, n, book_id;
List *book;
void AddToTail(List *book, Book *temp)
{
if (book->tail != NULL)
{
book->tail->next
= (struct Book *)malloc(sizeof(Book));
memcpy(book->tail->next,
temp, sizeof(Book));
book->tail = book->tail->next;
}
else
{
book->head = book->tail = (struct Book *)malloc(sizeof(Book));
memcpy(book->head, temp, sizeof(Book));
}
}
int main()
{
scanf("%d", &n);
book = (struct List *)malloc(sizeof(List));
book->head =
book->tail = NULL;
for (i = 0; i <
n; i++)
{
Book temp;
scanf("%d\n",
&temp.book_id);
gets(temp.title);
gets(temp.author);
gets(temp.subject);
AddToTail(book,
&temp);
}
scanf("%d", &q);
for (i = 0; i <
q; i++)
{
scanf("%d", &book_id);
int flag = 0;
Book *temp =
book->head;
for (j = 0; j <
n; j++)
{
if (book_id ==
temp->book_id)
{
printf("%s by
%s\n", temp->title, temp->author);
flag = 1;
break;
}
temp = temp->next;
}
if (flag == 0)
printf("Book not found\n");
}
return 0;
}